package cn.oa.service.impl;

import cn.oa.dao.AttendanceDao;
import cn.oa.dao.impl.AttendanceDaoImpl;
import cn.oa.entity.Attendance;
import cn.oa.service.AttendanceService;
import cn.oa.util.DatabaseUtil;
import cn.oa.util.Page;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AttendanceServiceImpl implements AttendanceService {

    @Override
    public int addSignInByEmployeeId(int employeeId,String date) throws SQLException {
        Connection conn = null;
        int result = 0;
        try{
            conn = DatabaseUtil.getConnection();
            conn.setAutoCommit(false);
            AttendanceDao attendanceDao = new AttendanceDaoImpl(conn);
            //查询员工是否有上班打卡记录
            Attendance record = attendanceDao.getRecordByEmployeeIdAndDate(employeeId, date);
            if(record==null){
                //执行打卡
                result = attendanceDao.addSinginByEmployeeId(employeeId);
            }else{
                //已完成上班打卡,不允许重复打卡
                result = 0;
            }
            conn.commit();
        }catch(SQLException e){
            e.printStackTrace();
            if(conn!=null){
                try{
                    conn.rollback();
                    result = -1;
                }catch(SQLException e1){
                    e.printStackTrace();
                }
            }
            throw e;
        }finally {
            DatabaseUtil.closeAll(conn,null,null);
        }
        return result;
    }

    @Override
    public int addSignOutByEmployeeId(int employeeId, String date) throws SQLException {
        Connection conn = null;
        int result = 0;
        try{
            conn = DatabaseUtil.getConnection();
            conn.setAutoCommit(false);
            AttendanceDao attendanceDao = new AttendanceDaoImpl(conn);
            //查询员工是否有上班打卡记录
            Attendance record = attendanceDao.getRecordByEmployeeIdAndDate(employeeId, date);
            if(record==null){
                //没有上班打卡记录时不允许执行下班打卡操作
                result = 0;
            }else{
                if(record.getSignOutTime()==null){
                    //执行下班打卡
                    result = attendanceDao.updateSingoutByAttendanceId(record.getId());
                }else{
                    result = -2;
                }
            }
            conn.commit();
        }catch(SQLException e){
            e.printStackTrace();
            if(conn!=null){
                try{
                    conn.rollback();
                    result = -1;
                }catch(SQLException e1){
                    e.printStackTrace();
                }
            }
            throw e;
        }finally {
            DatabaseUtil.closeAll(conn,null,null);
        }
        return result;
    }

    @Override
    public void getAttendancesByPage(String name, String searchDate, Page pageObj) throws SQLException {
        Connection conn = null;
        try {
            conn = DatabaseUtil.getConnection();
            AttendanceDao attendanceDao = new AttendanceDaoImpl(conn);
            int totalCount = attendanceDao.getAttendancesCount(name,searchDate);
            pageObj.setTotalCount(totalCount);
            if (totalCount > 0) {
                if (pageObj.getCurrPageNo() > pageObj.getTotalPageCount()) {
                    pageObj.setCurrPageNo(pageObj.getTotalPageCount());
                }
                List<Attendance> attendanceList = attendanceDao.getAttendancesByPage(name, searchDate,pageObj.getCurrPageNo(),pageObj.getPageSize());
                pageObj.setAttendanceList(attendanceList);
            } else {
                pageObj.setCurrPageNo(0);
                pageObj.setAttendanceList(new ArrayList<Attendance>());
            }

        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            DatabaseUtil.closeAll(conn,null,null);
        }
    }
}
